package com.northpool.operator.serve;

import com.alibaba.fastjson.JSONObject;
import fi.iki.elonen.NanoHTTPD;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

public class HttpSessionRestImpl implements IHttpSession {
    private final NanoHTTPD.IHTTPSession session;

    public HttpSessionRestImpl(NanoHTTPD.IHTTPSession session) {
        this.session = session;
    }

    @Override
    public Map<String, Object> getBodyParams() {
        Map<String, String> params = new LinkedHashMap<>();
        try {
            session.parseBody(params);
            String dataJson = params.get("postData");
            JSONObject jsonObject = JSONObject.parseObject(dataJson);
            return jsonObject.getInnerMap();
        } catch (IOException | NanoHTTPD.ResponseException e) {
            // 返回异常
            throw new RuntimeException(e);
        }
    }
}
